/****** Object:  Stored Procedure [dbo].spBienBanKiemKeKhoGetList Script Date: 7/16/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spDanhSachKhachHangConNo]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spDanhSachKhachHangConNo]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		17/07/2012	SongTM					Innit created
*******************************************************************************/

create procedure spDanhSachKhachHangConNo
	@songay int
as
	select distinct 
		kh.MaKH, kh.TenKH, kh.SoPX, kh.SoPG, kh.NgayGiao, kh.MaThuoc, kh.TenHoatChat, kh.TenBietDuoc, kh.SoLuong, kh.DonGia, tt.ThanhToan
	from v_KhachHangMuaHang as kh 
	left outer join v_KhachHangThanhToan as tt
	on kh.mactpx = tt.MaCTPX 
	Where (kh.SoLuong * kh.DonGia) > ISNULL(tt.ThanhToan, 0)
		  AND (DATEDIFF(day, getdate(), kh.NgayGiao) + 1) <= @songay
	
GO

